<?php

Class userController Extends baseController {

	public function index() { 
		
	}
	
	public function registration() { 
		if(!isset($_POST['username']))
		{
			$this->registry->template->show('registration');
			return;	
		}
		$user = new user();
		$user->TenDangNhap = trim($_POST['username']);
		$user->MatKhau = md5($_POST['passwd']);
		$user->HoTen = $_POST['fname'];
		$user->Email = trim($_POST['email']);
		$user->DienThoai = trim($_POST['mobile']);
		$user->DiaChi = trim($_POST['address']);
		$user->TrangThai = 1;
		$user->Quyen = 2;
		$user->Created_Date = date('Y-m-d'); //echo $user->Created_Date; die;
		$lstUser = $user->getUserByWhere('','');
		
		if(count($lstUser)>0)
		{
			//kiem tra neu ton tai ten dang nhap do roi thi hien thong bao
			foreach($lstUser as $u){
				if($u->TenDangNhap == $user->TenDangNhap){
					$this->registry->template->message_user = 'Tên đăng nhập đã tồn tại.';
					$this->registry->template->user = $user;
					$this->registry->template->show('registration');
					return;	
				}
			}
		
			//kiem tra neu ton tai email do roi thi hien thong bao
			foreach($lstUser as $u){
				if($u->Email == $user->Email){
					$this->registry->template->message_email = 'Email đã tồn tại.';
					$this->registry->template->user = $user;
					$this->registry->template->show('registration');
					return;	
				}
			}
		}
		$insert = $user->insertUser($user);
		if($insert)
			$this->registry->template->show('login');
		else echo "err";
	}
	
	public function login(){
		if(!isset($_POST['username']))
		{
			$this->registry->template->show('login');
			return;	
		}
		$username = $_POST['username'];
		$pass = md5($_POST['passwd']);
		$user = new user();
		$chkLogin = $user->checkLogin($username,$pass);
		if(count((array)$chkLogin) > 0)
		{
			
			$_SESSION['user'] = $chkLogin; //echo '<pre>'; var_dump($_SESSION['user']);die;
			
			echo '<script>parent.$.fancybox.close();parent.location.reload();</script>';
		}
		else
		{
			$this->registry->template->message = 'Thông tin đăng nhập không đúng.';
			$this->registry->template->show('login');
		}
			
	}
	
	public function logout(){
		unset($_SESSION['user']);	
		
	}
	
	public function user_info(){
		if(!isset($_SESSION['user'])){
			header('location:'.__URL_BASE.'trang-chu.html');
			exit;
		}
		//$this->registry->template->user = $_SESSION['user'];
		$this->registry->template->show('user_info');
	}
	
	public function profile_save(){
            $user = new user();
            $user->TenDangNhap = $_SESSION['user']->TenDangNhap;
			$user->MatKhau = $_SESSION['user']->MatKhau;
			$user->HoTen = $_POST['thoten'];
			$user->Email = $_POST['temail'];
			$user->DienThoai = $_POST['tdienthoai'];
			$user->DiaChi = $_POST['tdiachi'];
			$user->Quyen = $_SESSION['user']->Quyen;;
			$user->TrangThai = $_SESSION['user']->TrangThai;
            //echo '<pre>'; var_dump($user); die;
            $lstUser = $user->getUserByWhere('TenDangNhap <> "'.$user->TenDangNhap.'"','');
            if(count($lstUser)>0)
			{
			
				//kiem tra neu ton tai email do roi thi hien thong bao
				foreach($lstUser as $u){
					if($u->Email == $user->Email){
						$_SESSION['wrong'] = 'Email đã tồn tại.';
						header('location:'.__URL_BASE.'thong-tin-tai-khoan.html');
						exit;
					}
				}
			}
            $rs = $user->updateUser($user);
            if($rs){
                //echo '123';die;
                $_SESSION['change_info_success'] = 'Sửa hồ sơ thành công';
            }
			$_SESSION['user']->HoTen = $user->HoTen;
			$_SESSION['user']->Email = $user->Email;
			$_SESSION['user']->DienThoai = $user->DienThoai;
			$_SESSION['user']->DiaChi = $user->DiaChi;
            
            header('location:'.__URL_BASE.'thong-tin-tai-khoan.html');
			exit;
        }
		
		 public function change_pass_save(){
            $userModel = new user();
            $user = $_SESSION['user'];
            if($user->MatKhau != md5(trim($_POST['tpass']))){
                $_SESSION['wrong'] = 'Mật khẩu không đúng.';
                header('location:'.__URL_BASE.'thong-tin-tai-khoan.html');
                exit;
            }
            $user->MatKhau = md5(trim($_POST['tpassnew']));
            $rs = $userModel->updateUser($user);
            
            if($rs){
                //echo '123';die;
                $_SESSION['change_info_success'] = 'Sửa mật khẩu thành công';
            }
            header('location:'.__URL_BASE.'thong-tin-tai-khoan.html');
        }
}

?>
